<?php

namespace Unicom\Controller;

use Think\Controller;

class MiguController extends AdminController
{

    public function list_()
    {
        layout(false);
        $model = M('wa.pricecb_result', 'ivr_');

        $date=I('param.date',date('Y-m-d'));


        $sql = "select *,
 (select count(*) from wa.cmcc_migu_order where content=b.content and FROM_UNIXTIME(create_at, '%Y-%m-%d')=b.today and sync=1) as sync_count
 
from 
(select *,FROM_UNIXTIME(a.create_at, '%Y-%m-%d') as today,content as channel,count(*) as success_count
       
        
         from wa.cmcc_migu_order as a
        
        where FROM_UNIXTIME(a.create_at, '%Y-%m-%d')='$date'
        group by today,channel
        order by create_at desc) as b
       ";
        $data = $model->query($sql);

        $this->assign('date', $date);
        $this->assign('data', $data);

        $this->display();

    }

    public function find_()
    {
        layout(false);
        $model = M('wa.month', 'cp_');

        $phone = I('param.phone');

        $sql = "
        select a.*,FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at,p.dcprivence
        from wa.cmcc_migu_order as a
        left join cp.cp_privence as p on left (a.cal,7)=p.dcmobile
        where mobile='$phone'
        
        order by create_at desc 
        ";
        $data = $model->query($sql);


        $this->assign('data', $data);
        $this->display();

    }



    //成功详情

    public function info_($today, $channel)
    {
        layout(false);
        $model = M('wa.month', 'cp_');
        $today = I('param.today');
        $channel = I('param.channel');

        $sql = "
        select a.*,a.content as channel,FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at,p.dcprivence
        from wa.cmcc_migu_order as a
        left join cp.cp_privence as p on left (a.mobile,7)=p.dcmobile
        where FROM_UNIXTIME(create_at, '%Y-%m-%d')='$today' and content='$channel' 
        
        order by a.create_at desc 
        ";
        $data = $model->query($sql);


        $this->assign('data', $data);
        $this->display();

    }


    //上行不成功详情

    public function upErrorinfo_($today, $channel)
    {
        layout(false);
        $model = M('wa.ivr_pricing_param', '');
        $today = I('param.today');
        $channel = I('param.channel');

        $sql = "
        select a.*,FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at,p.dcprivence
        from wa.ivr_pricing_param as a
        left join cp.cp_privence as p on left (a.ext,7)=p.dcmobile
        where FROM_UNIXTIME(create_at, '%Y-%m-%d')='$today' and channel='$channel'
        
        order by a.create_at desc
        ";
        $data = $model->query($sql);


        $this->assign('data', $data);
        $this->display("upinfo_");

    }


    public function syncInfo_($today, $channel)
    {
        layout(false);
        $model = M('wa.month', 'cp_');
        $today = I('param.today');
        $channel = I('param.channel');

        $sql = "
        select a.*,a.content as channel,FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at,p.dcprivence
        from wa.cmcc_migu_order as a
        left join cp.cp_privence as p on left (a.mobile,7)=p.dcmobile
        where FROM_UNIXTIME(create_at, '%Y-%m-%d')='$today' and content='$channel' and sync=1
        
        order by a.create_at desc 
        ";
        $data = $model->query($sql);


        $this->assign('data', $data);
        $this->display("info_");

    }


    public function index()
    {
        layout(false);
        $this->assign('date', date('Y-m-d'));
        $this->display("privence");
    }

    public function statByPrivence_()
    {
        layout(false);
        $model = M('wa.month', 'cp_');
        $date = I('param.date', date('Y-m-d'), 'string');
        $channel = I('param.channel', 0);


        $sql = "
        select FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at,p.dcprivence,count(*) as count
        from wa.ivr_pricecb_result as a 
         left join cp.cp_privence as p on left (a.cal,7)=p.dcmobile
        where a.cod=200 and FROM_UNIXTIME(create_at, '%Y-%m-%d')='$date' 
        GROUP BY p.dcprivence order by count desc
        ";
        $sqlTotal = "select count(*) as total
        from wa.ivr_pricecb_result as a 
        
        where a.cod=200 and FROM_UNIXTIME(create_at, '%Y-%m-%d')='$date' ";
        $total_count = $model->query($sqlTotal)[0]['total'];
        $data = $model->query($sql);
        $result["total"] = $total_count;
        $result["data"] = $data;
        $this->assign("date",$date);
        $this->assign($result);
        $this->display("privence");

    }

    public function infoByChannel()
    {
        layout(false);
        $pdate = I('param.pdate', date("Y-m-d"));
        $pchannel = I('param.pchannel', 1011);
        $model = M();

        $sql1 = "select FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at,p.dcprivence,count(*) as count 
        from wa.ivr_pricing_param as a 
         left join cp.cp_privence as p on left (a.cal,7)=p.dcmobile
        where  FROM_UNIXTIME(create_at, '%Y-%m-%d')='$pdate' and a.channel=$pchannel
        GROUP BY p.dcprivence;";
        $sql3 = "select FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at,p.dcprivence,count(*) as count 
        from wa.ivr_pricecb_result as a 
         left join cp.cp_privence as p on left (a.cal,7)=p.dcmobile
        where a.cod=200  and a.sync=1 and FROM_UNIXTIME(create_at, '%Y-%m-%d')='$pdate' and a.channel=$pchannel
        GROUP BY p.dcprivence;";
        $sql2 = "select FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at,p.dcprivence,count(*) as count 
        from wa.ivr_code_param as a 
         left join cp.cp_privence as p on left (a.cal,7)=p.dcmobile
        where  a.code=200 and FROM_UNIXTIME(create_at, '%Y-%m-%d')='$pdate' and a.channel=$pchannel
        GROUP BY p.dcprivence;";

        $sql4 = "select FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at,p.dcprivence,count(*) as count 
        from wa.ivr_code_param as a 
         left join cp.cp_privence as p on left (a.cal,7)=p.dcmobile
        where   FROM_UNIXTIME(create_at, '%Y-%m-%d')='$pdate' and a.channel=$pchannel
        GROUP BY p.dcprivence;";
        $data1 = $model->query($sql1);

        $data2 = $model->query($sql2);

        $data3 = $model->query($sql3);
        $data4 = $model->query($sql4);
        $ret['pricing'] = $data1;
        $ret['code'] = $data2;
        $ret['code2'] = $data4;
        $ret['sync'] = $data3;
        $this->assign("pdate", $pdate);
        $this->assign("pchannel", $pchannel);
        $this->assign("data", $ret);
        $this->display("channel_info");


    }


    public function infoByPrivence()
    {
        layout(false);
        $date = I('param.date', date('Y-m'), 'string');
        $this->assign("date",$date);
        if (IS_GET) {

            $this->display("privence_count");

        } else {
            $model = M('wa.month', 'cp_');



            $sql = "
        select FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at,p.dcprivence,count(*) as count
        from wa.ivr_pricecb_result as a 
         left join cp.cp_privence as p on left (a.cal,7)=p.dcmobile
        where a.cod=200 and FROM_UNIXTIME(create_at, '%Y-%m')='$date' 
        GROUP BY p.dcprivence order by count desc
        ";
            $sqlTotal = "select count(*) as total
        from wa.ivr_pricecb_result as a 
        
        where a.cod=200 and FROM_UNIXTIME(create_at, '%Y-%m')='$date' ";
            $total_count = $model->query($sqlTotal)[0]['total'];
            $data = $model->query($sql);
            $result["total"] = $total_count;
            $result["data"] = $data;

            $this->assign($result);
            $this->display("privence_count");

        }


    }

}